//
//  EPMCategoryView.m
//  myXianfeng
//
//  Created by 马继鵬 on 17/5/2.
//  Copyright © 2017年 EPM. All rights reserved.
//

#import "EPMCategoryView.h"
#import "EPMSubCategory.h"
#import "EPMCategoryModel.h"

@interface EPMCategoryView()
@property (strong,nonatomic) NSMutableArray* btnList;
@property (weak,nonatomic) UIButton* selectedBtn;
@property (strong,nonatomic) MASConstraint* selfBottomConstaint;
@property (assign,nonatomic) NSInteger olderCount;
@property (strong,nonatomic) NSArray* subCatergorys;
//记录父类的index
@property (assign,nonatomic) NSInteger catergortIndex;

@end
@implementation EPMCategoryView

#pragma mark - 设置UI -


- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]){
        [self addNotification];
        EPMCategoryModel* model = _MarketManger.catergoryList[0];
        [self setupUIWithCategorys:model.cids];
    }
    return self;
}
#pragma mark - 添加通知 -
- (void)addNotification{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(marketCatergoryDidChangedNotification:) name:EPMMarketCatergoryDidChangedNotification object:nil];
    self.btnList = [NSMutableArray array];
}

static CGFloat margine = 6.0;
- (void)setupUIWithCategorys:(NSArray*)categorys{
    
    
    CGFloat height = 30.0;
    CGFloat width = (ScreenWidth* 0.75 - margine * 4)/3;
    for (int i = 0; i < 9; i ++){
        
        
        CGFloat btnX = margine + (margine + width) * (i % 3);
        CGFloat btnY = margine* 1.5 + (margine * 1.5 + height) * (i /3);
        CGRect frame = CGRectMake(btnX, btnY, width, height);
        UIButton* btn = [[UIButton alloc]initWithFrame:frame];
//        EPMSubCategory* subCategory = categorys[i];
        
        [self addSubview:btn];
//        [btn setTitle:subCategory.name forState:UIControlStateNormal];
        [btn  setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [btn setTitleColor:appColor forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [btn setBackgroundColor:lightBackColor];
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 10;
        btn.tag = i;
        [_btnList addObject:btn];
            
            //添加点击事件
            if (i == 0){
                btn.selected = YES;
                self.selectedBtn = btn;
                btn.backgroundColor = [UIColor yellowColor];
                [self mas_makeConstraints:^(MASConstraintMaker *make) {
                    self.selfBottomConstaint = make.bottom.equalTo(btn).offset(margine*1.5);
                }];
            }else{
                [self.selfBottomConstaint deactivate];
                [self mas_makeConstraints:^(MASConstraintMaker *make) {
                    self.selfBottomConstaint = make.bottom.equalTo(btn).offset(margine*1.5);
                }];
            }
            
            [btn addTarget:self action:@selector(actionSelectSubCaretgory:) forControlEvents:UIControlEventTouchUpInside];
    }
    [self updateBtnsWithCategorys:categorys];
       
}

#pragma mark - 通知方法 -
- (void)marketCatergoryDidChangedNotification:(NSNotification*)noti{
    NSInteger index = [noti.userInfo[KCatergoryChanged] integerValue];
    self.catergortIndex = index;
    EPMCategoryModel* catergory = _MarketManger.catergoryList[index];
    self.subCatergorys = catergory.cids;
    [self updateBtnsWithCategorys:catergory.cids];
}

- (void)updateBtnsWithCategorys:(NSArray*)categorys{
    
    NSInteger count = categorys.count;
    if (count < 2){
        
        
        for ( int i = 0 ; i < 9 ; i ++){
            UIButton* btn = self.btnList[i];
            btn.hidden = YES;
            if (i == 0){
                [self.selfBottomConstaint deactivate];
                [self mas_makeConstraints:^(MASConstraintMaker *make) {
                    self.selfBottomConstaint = make.bottom.equalTo(btn.mas_top).offset(-margine*1.5);
                }];
            }
        }
        if (self.updateConstraintBlock){
            self.updateConstraintBlock(YES);
        }
        
        
    }else{
    
    for ( int i = 0 ; i < 9 ; i ++){
        if ( i < count){
            UIButton* btn = self.btnList[i];
            btn.hidden = NO;
            EPMSubCategory* subModel = categorys[i];
            [btn setTitle:subModel.name forState:UIControlStateNormal];
            [self.selfBottomConstaint deactivate];
            [self mas_makeConstraints:^(MASConstraintMaker *make) {
                self.selfBottomConstaint = make.bottom.equalTo(btn).offset(margine*1.5);
            }];
            if (i == 0){
                btn.selected = YES;
                self.selectedBtn = btn;
            }
        }else{
            UIButton* btn = self.btnList[i];
            btn.hidden = YES;

        }
     }
        if (self.updateConstraintBlock){
            self.updateConstraintBlock(NO);
        }
    }
    
}


- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}


- (void)setSubCatergorys:(NSArray *)subCatergorys{
    _subCatergorys = subCatergorys;
    
    
}

- (void)actionSelectSubCaretgory:(UIButton*)sender{
    if ( self.selectedBtn != sender){
        self.selectedBtn.selected = NO;
        self.selectedBtn.backgroundColor = [UIColor lightGrayColor];
        self.selectedBtn = sender;
        sender.backgroundColor = [UIColor yellowColor];
        sender.selected = YES;
    }
    NSInteger index = sender.tag;
    EPMSubCategory* subCategory = self.subCatergorys[index];
    NSInteger cid =[subCategory.id intValue];
        
    [[NSNotificationCenter defaultCenter]postNotificationName:EPMMaketSubCatergoryDidChangedNotification object:nil userInfo:@{KCatergoryChanged:@(self.catergortIndex),KSubCatergoryChanged:@(cid)}];
    
    
}


@end
